<template>
  <d2-container>
    <el-form :inline="true" :model="dataSearch" ref="dataSearch">
     <!--  <el-form-item label="教室名称" prop="name">
        <el-input v-model="dataSearch.name" placeholder="请输入教室名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="formSubmit">
          <d2-icon name="search" />查询
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="formReset">
          <d2-icon name="refresh" />重置
        </el-button>
      </el-form-item> -->
      <a href="#/holiday-create">
        <!-- <el-button type="primary" style="float:right;margin-bottom: 2vw" icon="el-icon-plus" v-if='headquarter'>新建节假日</el-button> -->
         <el-button type="primary" style="float:right;margin-bottom: 2vw" icon="el-icon-plus">新建节假日</el-button>

      </a>
    </el-form>
    <el-table :data="dataList" border style="width: 100%">
      <el-table-column prop="name" label="假期名称"></el-table-column>
      <el-table-column prop="description" label="假期名称"></el-table-column>
      <el-table-column prop="startTime" label="开始时间"></el-table-column>
      <el-table-column prop="endTime" label="结束时间"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <!-- <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button> -->
          <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
                    <!-- <el-button @click="deleteClick(scope.row)" type="text" size="small" v-if='headquarter'>删除</el-button> -->

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
      @next-click="handleNextPage"
      @prev-click="handlePrevClick"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import { mapState } from "vuex";

export default {
  data() {
    return {
      dataList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      classroom: [],
      dataSearch: {},
      headquarter : 0
    };
  },
      computed:{
    ...mapState('d2admin/user',['info'])
  },
  methods: {
    formSubmit() {
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    formReset() {
      this.$refs.dataSearch.resetFields();
    },
    handleClick(e) {
      this.$router.push({ path: "/holiday-create?id=" + e._id });
    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlerSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handleNextPage() {
      this.currentPage = this.currentPage + 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlePrevClick() {
      this.currentPage = this.currentPage - 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    deleteClick(e) {

      this.$confirm("此操作将删除该节日, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/holidayRecord/deleteHolidayRecord",
            method: "post",
            data: { id: e._id }
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "删除成功"
              });
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    getDataList(data) {
      var datax = data
      datax.school = this.school
      request({
        url: "/holidayRecord/getHolidayRecordList",
        method: "post",
        data: datax
      }).then(res => {
        this.dataList = [];
        this.dataList = res.data.holiday;
        this.dataList.forEach((item, index) => {
          this.dataList[index].startTime = this.formatDate(item.startTime);
          this.dataList[index].endTime = this.formatDate(item.endTime);

          this.dataList[index].department = item.department.map(value => value.name).join(",")
          this.dataList[index].staff = item.staff.map(value => value.name).join(",")
          this.dataList[index].outStaff = item.outStaff.map(value => value.name).join(",")
        });
        this.total = res.data.total;
        this.currentPage = res.data.currentPage;
      });

    },
    formatDate(date) {
      date = new Date(date);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return [year, month, day].map(this.formatNumber).join("-");
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    },
    load(){
      this.dataSearch.page = this.currentPage;
    this.dataSearch.pageSize = this.pageSize;
    this.school = this.info.school[0]._id
    this.getDataList(this.dataSearch);
    if ((this.info.school[0].headquarter == 1) || (this.info.school[0].headquarter == 2)){
      this.headquarter = 1
    }
    this.rights = this.info.rights.map(function(e){return e.name})
            let index1 = this.rights.indexOf('root')
    if (index1 != -1){
                this.headquarter = 1
    }
    }
  },
  beforeRouteEnter(to, from, next) {
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }


        },
        beforeRouteLeave(to,from,next){
            next()
        },
  created() {
    this.load()

  }
};
</script>
